package com.kkwrite.regimen.provider.controller.login;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.login.ChangePasswordInDTO;
import com.kkwrite.regimen.common.entity.dto.login.CheckPasswordInDTO;
import com.kkwrite.regimen.common.entity.dto.login.ShopLoginInDTO;
import com.kkwrite.regimen.provider.service.login.LoginService;
import com.kkwrite.regimen.provider.util.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;
import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_TRUE;

/**
 * Created by lisha on 2018/8/16 11:43.
 *
 * @author lisha
 */
@RestController
@RequestMapping(value = "/login")
@Api(value = "登录模块")
public class LoginController {

    @Resource(name = "loginService")
    private LoginService loginService;
    
    @Resource(name = "tokenUtil")
    private TokenUtil tokenUtil;

    @PostMapping(value = "/adminlogin")
    @ApiOperation(value = "店铺登录接口", notes = "该接口返回token")
    @ApiImplicitParam(name = "inDTO", value = "用户名和手机号二选一，密码必传，code为验证码，当前不需要，可为空",
            required = true, paramType = "body", dataType = "ShopLoginInDTO")
    public OutDTO shopLogin(@RequestBody ShopLoginInDTO inDTO) {
        if (inDTO.isInvalid()) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        try {
            return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK", loginService.shopLogin(inDTO));
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "异常信息：" + e.getMessage());
        }
    }
    
    @PostMapping(value = "/checkPassword")
    @ApiOperation(value = "验证密码接口", notes = "返回是否正确success为正确，false为不正确")
    @ApiImplicitParam(name = "inDTO", value = "密码为必传项",
            required = true, paramType = "body", dataType = "CheckPasswordInDTO")
    public OutDTO checkPassword(@RequestBody CheckPasswordInDTO inDTO,HttpServletRequest request) {
        if (inDTO.getPassword()==null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        Object obj = tokenUtil.getValue(request.getHeader("token"), TokenUtil.TokenKeyEnum.USERNAME);
		if (obj == null) {
			return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid");
		}
		String username = (String) obj;
		inDTO.setUsername(username);
        try {
            return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK", loginService.checkPassword(inDTO));
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "异常信息：" + e.getMessage());
        }
    }
    
    @PostMapping(value = "/changePassword")
    @ApiOperation(value = "修改密码接口", notes = "返回是否正确success为修改成功")
    @ApiImplicitParam(name = "inDTO", value = "新密码和确认密码为必传项",
            required = true, paramType = "body", dataType = "ChangePasswordInDTO")
    public OutDTO changePassword(@RequestBody ChangePasswordInDTO inDTO,HttpServletRequest request) {
        String password = inDTO.getPassword();
        String passwordVerify = inDTO.getPasswordVerify();
        if(password==null||passwordVerify==null){
        	return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        if(!password.equals(passwordVerify)){
        	return OutDTO.of(BOOLEAN_STRING_FALSE, "两次密码输入不一致");
        }
        if(password.length()>8){
        	return OutDTO.of(BOOLEAN_STRING_FALSE, "密码长度小于8位");
        }
        Object obj = tokenUtil.getValue(request.getHeader("token"), TokenUtil.TokenKeyEnum.USERNAME);
		if (obj == null) {
			return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid");
		}
		String username = (String) obj;
		inDTO.setUsername(username);
        try {
            return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK", loginService.changePassword(inDTO));
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "异常信息：" + e.getMessage());
        }
    }

}
